package com.xm.servlet.reception_desk.set_up;

import com.xm.entity.AdministratorInformation;
import com.xm.service.background.AdministratorInformationService;
import lombok.SneakyThrows;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author 田顺琪
 * @date 2024-01-17
 */
@WebServlet(name = "SetServlet", value = "/set")
public class SetServlet extends HttpServlet {
    final AdministratorInformationService administratorInformationService = new AdministratorInformationService();

    @SneakyThrows
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String name = request.getParameter("name");
        String phone = request.getParameter("phone");
        String signature=null;
        String sign= request.getParameter("signature");
        if("".equals(sign) && null==sign){
            signature=null;
        }else {
            signature=sign;
        }
        String sex = request.getParameter("sex");
        String avatar = request.getParameter("avatar");
        int gender=0;
        String man="男";
        String woman="女";
        if(sex.equals(man)){
            gender=0;
        } else if (sex.equals(woman)) {
            gender=1;
        }
        String mailbox = request.getParameter("mailbox");
        int informationId = Integer.parseInt(request.getParameter("informationId"));
        String sparePhone = request.getParameter("spar;.ePhone");
        String homeAddress = request.getParameter("homeAddress");
        String contact = request.getParameter("contact");
        String contactPhone = request.getParameter("contactPhone");
        String administratorId = request.getParameter("administratorId");
        String dateBirth = request.getParameter("dateBirth");
        Date newDate=null;
        if(!"".equals(dateBirth)){
            String date=request.getParameter(dateBirth);
            //日期转换
            SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm;ss");
            try {
                newDate =sdf.parse(date);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        String department = request.getParameter("departmentId");
        int departmentId=0;
        if("".equals(department)){
            departmentId=0;
        }
        AdministratorInformation administratorInformation=new AdministratorInformation(name,phone,mailbox,informationId,sparePhone,homeAddress,contact,signature,contactPhone,gender,avatar,administratorId,newDate,departmentId);
        boolean ret=administratorInformationService.addAdministratorInformation(administratorInformation);
        response.getWriter().print(ret);
    }
}
